﻿using System;
using System.Collections.Generic;
using System.Text;
using Pixysoft.Framework.Reflection;
using Pixysoft.Weblications.Sango.Protocol.player;
using Pixysoft.Weblications.Sango.Environment;
using Pixysoft.Weblications.Sango.Dao;
using Pixysoft.Weblications.Sango.Protocol;

namespace Pixysoft.Weblications.Sango.Command.officer
{
    partial class SangoOfficerHandler
    {
        private const string default_null_position = "NULL";

        public officer_tacticalsave officer_tacticalsave(long tacticaltypecode, List<string> positions)
        {
            string userCode = SangoManager.Authentication();

            officer_tacticalsave response = ReflectionManager.CreatePojo<officer_tacticalsave>();
            response.Succeed = false;

            Entities.Usr_Tactical defaulttactical = TacticalDao.getUsrTactical(userCode, tacticaltypecode);

            List<Entities.Usr_Tacticalpos> defaultpositions = TacticalDao.getUserTacticalPositions(defaulttactical.Primarycode);
            if (defaultpositions.Count != positions.Count)
            {
                response.Message = "传入参数错误";
                return response;
            }

            foreach (Entities.Usr_Tacticalpos pos in defaultpositions)
            {
                string officerprimarycode = positions[defaultpositions.IndexOf(pos)];

                if (string.Equals(officerprimarycode, default_null_position, StringComparison.OrdinalIgnoreCase))
                    pos.Officerprimarycode = null;
                else
                    pos.Officerprimarycode = officerprimarycode;
            }
            IEntityNoebeTransaction command = SettingHelper.EntityTransaction;
            command.Begin(delegate()
            {
                command.Update(defaultpositions.ToArray());

                command.Commit();
            });


            List<Entities.Usr_Tactical> tacticals = TacticalDao.getUserTacticals(userCode);
            List<Entities.Usr_Strategy> strategies = StrategyDao.getStrategies(userCode);
            List<player_tactical> tlist = new List<player_tactical>();
            foreach (Entities.Usr_Tactical ormtactical in tacticals)
            {
                Entities.Sys_Tactical sysTactical = SysTacticalHelper.search(ormtactical.Tacticaltypecode);
                Entities.Usr_Strategy strategy = StrategyDao.searchStrategy(strategies, sysTactical.Strategytypecode);
                tlist.Add(UsrTacticalFacade.ToTactical(
                    strategy,
                    ormtactical,
                    TacticalDao.getUserTacticalPositions(ormtactical.Primarycode)));
            }

            response.tacticals = tlist;
            response.Succeed = true;
            return response;
        }


    }
    public interface officer_tacticalsave : ISangoResponse
    {
        List<player_tactical> tacticals { get;set;}
    }
}
